import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class POIExcelRead {
	/**
	 * 对外提供读取excel 的方法
	 * 
	 * @throws IOException
	 * */
	boolean isExcel2007 = false;
	HSSFSheet sheet2003 = null;
	XSSFSheet sheet2007 = null;

	private HSSFSheet getSheet2003() {
		return sheet2003;
	}

	private void setSheet2003(HSSFSheet sheet2003) {
		this.sheet2003 = sheet2003;
	}

	private XSSFSheet getSheet2007() {
		return sheet2007;
	}

	private void setSheet2007(XSSFSheet sheet2007) {
		this.sheet2007 = sheet2007;
	}

	public static void main(String[] args) throws Exception {
		File a = new File("c:\\2007.xlsx");
		POIExcelRead r = new POIExcelRead(a);
		String []title = r.getRowData(1);
		for(String v:title){
			System.out.println(v);
		}
	}

	public POIExcelRead(File a) throws Exception {
		readExcel(a);
	}

	private void readExcel(File file) throws Exception {
		String fileName = file.getName();
		String extension = fileName.lastIndexOf(".") == -1 ? "" : fileName
				.substring(fileName.lastIndexOf(".") + 1);
		if ("xls".equals(extension)) {
			read2003Excel(file);
		} else if ("xlsx".equals(extension)) {
			isExcel2007 = true;
			read2007Excel(file);
		} else {
			throw new IOException("不支持的文件类型");
		}
	}

	public int getTotalColumns() {
		return isExcel2007 ? getSheet2007().getRow(1)
				.getPhysicalNumberOfCells() : getSheet2003().getRow(1)
				.getPhysicalNumberOfCells();
	}

	public int getTotalRows() {
		return isExcel2007 ? getSheet2007().getLastRowNum() + 1
				: getSheet2003().getLastRowNum() + 1;
	}

	public String[] getRowData(int index) throws Exception {
		return isExcel2007 ? getRowData2007(index) : getRowData2003(index);
	}

	private String[] getRowData2003(int rownum) throws Exception {
		HSSFSheet sheet = getSheet2003();
		if (sheet == null) {
			return null;
		}
		String[] columnTitle = null;
		String value = null;
		HSSFRow row = null;
		HSSFCell cell = null;
		row = sheet.getRow(rownum);
		if (row == null) {
			throw new Exception("excel文件的第" + (rownum + 1) + "行为空");
		}
		int totalColumnNumber = row.getLastCellNum();
		columnTitle = new String[totalColumnNumber];
		for (int j = row.getFirstCellNum(); j <= totalColumnNumber - 1; j++) {
			cell = row.getCell(j);
			if (cell == null) {
				columnTitle[j] = "N";
			}
			 
			columnTitle[j] = cell.getStringCellValue().trim();
			if (value == null || "".equals(value)) {
				columnTitle[j] = "N";
			}
		}
		return columnTitle;
	}

	private String[] getRowData2007(int rownum) throws Exception {
		XSSFSheet sheet = getSheet2007();
		if (sheet == null) {
			return null;
		}
		String[] rowDates = null;
		XSSFRow row = null;
		XSSFCell cell = null;

		row = sheet.getRow(rownum);
		if (row == null) {
			throw new Exception("excel文件的第" + (rownum + 1) + "行为空");
		}

		int totalColumnNumber = row.getLastCellNum();
		rowDates = new String[totalColumnNumber];

		for (int j = row.getFirstCellNum(); j < totalColumnNumber; j++) {
			cell = row.getCell(j);
             System.out.println("当前cell =  "+cell.getStringCellValue().trim());
			if (cell != null) {
				rowDates[j] = cell.getStringCellValue().trim();
			}
			if (cell == null) {
				rowDates[j] = "N";
			}
			if (rowDates[j] == null || "".equals(rowDates[j])) {
				rowDates[j] = "N";
			}
			
		}
		return rowDates;
	}

	/**
	 * 读取 office 2003 excel
	 * 
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	private void read2003Excel(File file) throws Exception {
		HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));
		HSSFSheet sheet = hwb.getSheetAt(0);
		setSheet2003(sheet);

		if (sheet == null) {
			throw new Exception("excel文件为空");
		}
	}

	/**
	 * 读取Office 2007 excel及2007以上excel
	 * 
	 * @throws Exception
	 * */
	private void read2007Excel(File file) throws Exception {
		String line = "";
		// 构造 XSSFWorkbook 对象，strPath 传入文件路径
		XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
		// 读取第一章表格内容
		XSSFSheet sheet = xwb.getSheetAt(0);
		setSheet2007(sheet);
		if (sheet == null) {
			throw new Exception("excel文件为空");
		}
	}
}